<template>
  <div>
    <div style="padding: 15px 0">
      <el-button type="primary" size="small" @click="add">添加商品</el-button>
      <div style="float:right">
        <el-input placeholder="ID/名称/货号" v-model="searchName" size="small" style="min-width:280px">
          <el-select v-model="goodsType" slot="prepend" placeholder="请选择" size="small" style="min-width:100px">
            <el-option label="未选择" value=""></el-option>
            <el-option v-for="item in goodsTypeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
          <el-button slot="append" icon="el-icon-search" size="small" @click="search"></el-button>
        </el-input>
      </div>
    </div>

    <!-- 表格 -->
    <el-tabs v-model="status" @tab-click="handleClick">
      <el-tab-pane label="上架" name="1">
        <el-table :data="goodsList" style="width:100%" stripe>
          <el-table-column prop="id" label="商品ID" align="center" width="100px"></el-table-column>
          <el-table-column label="商品主图" align="center">
            <template slot-scope="scope" width="80px">
              <div v-if="scope.row.pic_urls!==''">     
                <!-- eslint-disable-next-line  -->
                <l-pic v-for="(item, index) in scope.row.pic_urls.split(',')" :key="index" v-if="item !== ''"
                  :picurl="item" :size="{width:40,height:40}" :disdel="false" :disview="false"/>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="商品名称" align="center">
            <template slot-scope="scope">
              <div>
                <div>{{ scope.row.name }}</div>
                <!-- <div style="color:#2d8cf0">[{{ scope.row.m_category_name }}]</div> -->
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="score" label="积分" align="center" width="80px"></el-table-column>
          <el-table-column prop="stocks" label="库存" align="center" width="80px"></el-table-column>
          <el-table-column label="属性" align="center" width="180px">
            <template slot-scope="scope">
              <div>
                <el-tag type="info" effect="dark" size="mini">预售</el-tag>
                <el-tag :type="scope.row.is_limit === '1' ? '' : 'info'" effect="dark" size="mini">限量</el-tag>
                <el-tag type="info" effect="dark" size="mini">秒杀</el-tag>
                <el-tag :type="scope.row.is_open_assemble === '1' ? '' : 'info'" effect="dark" size="mini">拼团</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="是否上架" align="center">
            <template slot-scope="scope" width="80px">
              <el-switch v-model="scope.row.status" active-color="#13ce66" active-value="1" inactive-value="0" @change="changeStatus(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="create_time" label="创建时间" align="center"></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              
              <el-button class="action-button" type="text" 
                @click="edit(scope.row)"><i class="el-icon-edit"/></el-button>
              <el-button class="action-button" 
                @click="del(scope.row)" type="text">
                <svg-icon icon-class="shanchu"/>
              </el-button>
              <el-button class="action-button" type="text"><svg-icon icon-class="二维码"/></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="下架" name="0">
        <el-table :data="goodsList" style="width:100%" stripe>
          <el-table-column prop="id" label="商品ID" align="center" width="100px"></el-table-column>
          <el-table-column label="商品主图" align="center">
            <template slot-scope="scope" width="80px">
              <div v-if="scope.row.pic_urls!==''">     
                <!-- eslint-disable-next-line  -->
                <l-pic v-for="(item, index) in scope.row.pic_urls.split(',')" :key="index" v-if="item !== ''"
                  :picurl="item" :size="{width:40,height:40}" :disdel="false" :disview="false"/>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="商品名称" align="center">
            <template slot-scope="scope">
              <div>
                <div>{{ scope.row.name }}</div>
                <div style="color:#2d8cf0">[{{ scope.row.m_category_name }}]</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="price" label="价格" align="center" width="80px"></el-table-column>
          <el-table-column prop="stocks" label="库存" align="center" width="80px"></el-table-column>
          <el-table-column prop="" label="销量" align="center" width="80px"></el-table-column>
          <el-table-column label="属性" align="center" width="180px">
            <template slot-scope="scope">
              <div>
                <el-tag type="info" effect="dark" size="mini">预售</el-tag>
                <el-tag :type="scope.row.is_limit === '1' ? '' : 'info'" effect="dark" size="mini">限量</el-tag>
                <el-tag type="info" effect="dark" size="mini">秒杀</el-tag>
                <el-tag :type="scope.row.is_open_assemble === '1' ? '' : 'info'" effect="dark" size="mini">拼团</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="是否上架" align="center">
            <template slot-scope="scope" width="80px">
              <el-switch v-model="scope.row.status" active-color="#13ce66" active-value="1" inactive-value="0" @change="changeStatus(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="create_time" label="创建时间" align="center"></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              
              <el-button class="action-button" type="text" 
                @click="edit(scope.row)"><i class="el-icon-edit"/></el-button>
              <el-button class="action-button" 
                @click="del(scope.row)" type="text">
                <svg-icon icon-class="shanchu"/>
              </el-button>
              <el-button class="action-button" type="text"><svg-icon icon-class="二维码"/></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <!-- 分页 -->
    <div style="text-align:right;margin:15px 15px">
      <el-pagination class="page"
        background
        layout="total, prev, pager, next, jumper"
        :total="count"
        :page-size="10"
        :current-page="page"
        @size-change="changePage"
        @current-change="changePage">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getGoodsList, getScoreGoodsList, putGoods, delScoreGoods } from '@/api/score'
// import { getScoreGoodsList } from '@/api/addgoods'
import LPic from '@/components/lPic'

export default {
  name: 'goodsList',
  components: {
    LPic
  },
  data() {
    return {
      page: 1,
      count: 1,
      status: '1',
      goodsList: [],
      searchName: '',
      goodsType: '',
      goodsTypeList: [],
    }
  },
  mounted() {
    this.getGoodsList();
    this.getGoodsType();
  },
  methods: {
    /**
     * 获取商品列表
     */
    getGoodsList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        limit: 10,
        page: this.page,
        status: this.status
      };
      getGoodsList(params).then(res => {
        if(res.status === 200) {
          this.goodsList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204) {
          this.goodsList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 翻页
     */
    changePage(page) {
      this.page = page;
      this.getGoodsList();
    },
    /**
     * 修改商品上架状态
     */
    changeStatus(data) {
      const params = {
        id: data.id,
        key: this.$store.state.app.activeApp.saa_key,
        // start_time: data.status === '1' ? data.start_time : '',
        status: data.status
      };

      putGoods(params).then(res => {
        if(res.status === 200) {
          this.getGoodsList();
          this.$message.success('修改成功！');
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 添加
     */
    add() {
      this.$router.push('/score/list/add');
    },
    /**
     * 编辑
     */
    edit(data) {
      this.$router.push('/score/list/add?id='+data.id);
    },
    /**
     * 删除
     */
    del(data) {
      const params = { key: this.$store.state.app.activeApp.saa_key, id: data.id };
      delScoreGoods(params).then(res => {
        if(res.status === 200) {
          this.$message.success('删除成功！');
          this.getGoodsList();
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 获取商品类型
     */
    getGoodsType() {
      getScoreGoodsList({key: this.$store.state.app.activeApp.saa_key}).then(res => {
        if(res.status === 200) {
          this.goodsTypeList = res.data;
        }else if(res.status === 204){
          this.goodsTypeList = [];
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 搜索
     */
    search() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        limit: 10,
        page: 1,
        status: 1,
        searchName: this.searchName,
        m_category_id: this.goodsType,
      };

      if(params.m_category_id === ""){
        delete params.m_category_id;
      }

      getGoodsList(params).then(res => {
        if(res.status === 200) {
          this.goodsList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204) {
          this.goodsList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    handleClick() {
      this.getGoodsList();
    }
  },
}
</script>

<style lang="scss" scoped>
.goods-list-container{
  min-height: calc(100vh - 100px);
  margin: 20px;
  background-color: #fff;
  padding:25px;
  box-shadow: 0px 0px 4px 0px rgba(115, 115, 115, 0.2);
}
</style>